package com.crm.service.impl;

import com.crm.dao.PermissionMapper;
import com.crm.dao.RoleMapper;
import com.crm.dao.UserMapper;
import com.crm.domain.DataGridView;
import com.crm.domain.TreeNode;
import com.crm.pojo.Permission;
import com.crm.pojo.User;
import com.crm.service.UserService;
import com.crm.util.TreeNodeBuilder;
import com.crm.vo.UserVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Override
    public List<User> findAllUsers() {
        return userMapper.selectAllUsers();
    }

    @Override
    public User login(String telephone, String password) {
        return userMapper.selectUserByTelAndPwd(telephone,password);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addUser(User user) {
        boolean flag=false;
        String algorithmName="MD5";
        Object salt=null;
        int hashIterations=1024;
        Object encryptionPwd=new SimpleHash(algorithmName,user.getPassword(),salt,hashIterations);
        user.setPassword(encryptionPwd.toString());
        this.userMapper.insertSysUser(user);
        flag=true;
        return flag;
    }

    @Override
    public User findUserByTel(String telephone) {
        return this.userMapper.selectUserByTel(telephone);
    }

    @Override
    public Set<String> selectRoleNamesByUid(int uid) {
        return roleMapper.selectRoleNamesByUid(uid);
    }

    @Override
    public Set<String> selectPercodesByUid(int uid) {
        return permissionMapper.selectPercodesByUid(uid);
    }

    @Override
    public DataGridView getUserListByPage(UserVo userVo) {
        Page<Object> page= PageHelper.startPage(userVo.getPage(),userVo.getLimit());
        List<User> data=this.userMapper.selectUsersByProperty(userVo);
        return new DataGridView(page.getTotal(),data);
    }
    @Transactional
    @Override
    public void removeUser(Integer id) {
        this.userMapper.deleteByPrimaryKey(id);
        this.roleMapper.deleteRoleUserByUid(id);
    }
    @Transactional
    @Override
    public void removeBatchUser(Integer[] ids) {
        for(Integer id:ids){
            this.removeUser(id);
        }
    }
    @Transactional
    @Override
    public void assignRole(Integer[] rids, Integer uid) {
        this.userMapper.deleteRoleAndUserByUid(uid);
        for(Integer rid:rids){
            this.userMapper.insertSysUserRole(uid,rid);
        }
    }

    @Override
    public List<TreeNode> selectMenusByUid(int uid) {
        List<TreeNode> treeNodes=new ArrayList<>();
        List<Permission> permissionList=this.permissionMapper.selectMenusByUid(uid);
        for(Permission permission:permissionList){
            TreeNode treeNode=new TreeNode();
            treeNode.setId(permission.getId());
            treeNode.setPid(permission.getPid());
            treeNode.setTitle(permission.getName());
            treeNode.setIcon(permission.getIcon());
            treeNode.setHref(permission.getHref());
            treeNode.setSpread(permission.getOpen()==1?true:false);
            treeNodes.add(treeNode);
        }
        return TreeNodeBuilder.build(treeNodes,1);
    }
}
